package org.waddy.framework.pub.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.springframework.jdbc.core.RowCallbackHandler;
import org.waddy.framework.app.exception.AppException;
import org.waddy.framework.app.exception.SysException;
import org.waddy.framework.app.model.dao.Sql;
import org.waddy.framework.app.model.dao.impl.DAOImpl;
import org.waddy.framework.pub.dao.IMaxIdDAO;
import org.waddy.framework.pub.util.common.StringUtil;



public class MaxIdDAOImpl extends DAOImpl implements IMaxIdDAO {

	public String getSequenceNextVal(String seqName) throws SysException,
			AppException {
		String seq = "0";
		Sql sql = new Sql();
		sql.append("select " + seqName + "_SEQ.nextval from dual");
		sql.log(this.getClass());
		List<Object> params = new ArrayList<Object>();
		final List data = new ArrayList();
		try {
			jdbcTemplate.query(sql.getSql(), params.toArray(),
					new RowCallbackHandler() {
						public void processRow(ResultSet rs)
								throws SQLException {
							Object obj = rs.getString(1);
							data.add(obj);
						}
					});
			if (data.size() > 0) {
				seq = (String) data.get(0);
			}
		} catch (Exception e) {
			throw new SysException("100000", "查找序列" + seqName + "的最大值失败", e);
		}
		return seq;
	}

	public Long getSequenceNextVal4Long(String seqName) throws SysException,
			AppException {
		Long seq = (long) 0;
		Sql sql = new Sql();
		sql.append("select " + seqName + "_SEQ.nextval from dual");
		sql.log(this.getClass());
		List<Object> params = new ArrayList<Object>();
		final List data = new ArrayList();
		try {
			jdbcTemplate.query(sql.getSql(), params.toArray(),
					new RowCallbackHandler() {
						public void processRow(ResultSet rs)
								throws SQLException {
							Object obj = rs.getLong(1);
							data.add(obj);
						}
					});
			if (data.size() > 0) {
				seq = (Long) data.get(0);
			}
		} catch (Exception e) {
			throw new SysException("100000", "查找序列" + seqName + "的最大值失败", e);
		}
		return seq;
	}

	public Long getMaxIdFromTable(String tableName, String columnName,
			String condition) throws SysException, AppException {
		Long seq = null;
		Sql sql = new Sql();
		//sql.append("select nvl(max("); //oracle
		sql.append("select ifnull(max("); //mysql
		sql.append(columnName);
		sql.append("+0),0) from ");
		sql.append(tableName);

		if (!StringUtil.isBlank(condition)) {
			sql.append(" where ");
			sql.append(condition);
		}
		sql.log(this.getClass());
		List<Object> params = new ArrayList<Object>();
		final List data = new ArrayList();
		try {
			jdbcTemplate.query(sql.getSql(), params.toArray(),
					new RowCallbackHandler() {
						public void processRow(ResultSet rs)
								throws SQLException {
							Object obj = new Long(rs.getLong(1));
							data.add(obj);

						}
					});
			if (data.size() > 0) {
				seq = (Long) data.get(0);
			}

		} catch (Exception ex) {
			throw new SysException("", "查找" + tableName + "表字段" + columnName
					+ "的最大值失败" + "sql is " + sql, ex);
		}
		return seq;
	}

	

}
